package com.fuxinglianbao.controllers;

import com.fuxinglianbao.common.ApiResponse;
import com.fuxinglianbao.models.User;
import com.fuxinglianbao.services.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    @PostMapping("/send-code")
    public ApiResponse<Void> sendVerificationCode(@RequestParam String phone) {
        authService.sendVerificationCode(phone);
        return ApiResponse.success("验证码发送成功", null);
    }

    @PostMapping("/register")
    public ApiResponse<User> register(
            @RequestParam String phone,
            @RequestParam String password,
            @RequestParam String code,
            @RequestParam(required = false) String referrerPhone) {
        User user = authService.registerByPhone(phone, password, code, referrerPhone);
        return ApiResponse.success("注册成功", user);
    }

    @PostMapping("/login/password")
    public ApiResponse<User> loginByPassword(
            @RequestParam String phone,
            @RequestParam String password) {
        User user = authService.loginByPassword(phone, password);
        return ApiResponse.success("登录成功", user);
    }

    @PostMapping("/login/code")
    public ApiResponse<User> loginByCode(
            @RequestParam String phone,
            @RequestParam String code) {
        User user = authService.loginByCode(phone, code);
        return ApiResponse.success("登录成功", user);
    }
}